﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices;

    public class SysShop
    {
        private readonly Chain.IDAL.SysShop dal = new Chain.IDAL.SysShop();

        public int Add(Chain.Model.SysShop model)
        {
            if (this.Exists(model.ShopName))
            {
                return -1;
            }
            return this.dal.Add(model);
        }

        public List<Chain.Model.SysShop> DataTableToList(DataTable dt)
        {
            List<Chain.Model.SysShop> list = new List<Chain.Model.SysShop>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.SysShop item = new Chain.Model.SysShop();
                    if ((dt.Rows[i]["ShopID"] != null) && (dt.Rows[i]["ShopID"].ToString() != ""))
                    {
                        item.ShopID = int.Parse(dt.Rows[i]["ShopID"].ToString());
                    }
                    if ((dt.Rows[i]["ShopName"] != null) && (dt.Rows[i]["ShopName"].ToString() != ""))
                    {
                        item.ShopName = dt.Rows[i]["ShopName"].ToString();
                    }
                    if ((dt.Rows[i]["ShopTelephone"] != null) && (dt.Rows[i]["ShopTelephone"].ToString() != ""))
                    {
                        item.ShopTelephone = dt.Rows[i]["ShopTelephone"].ToString();
                    }
                    if ((dt.Rows[i]["ShopContactMan"] != null) && (dt.Rows[i]["ShopContactMan"].ToString() != ""))
                    {
                        item.ShopContactMan = dt.Rows[i]["ShopContactMan"].ToString();
                    }
                    if ((dt.Rows[i]["ShopAreaID"] != null) && (dt.Rows[i]["ShopAreaID"].ToString() != ""))
                    {
                        item.ShopAreaID = int.Parse(dt.Rows[i]["ShopAreaID"].ToString());
                    }
                    if ((dt.Rows[i]["ShopAddress"] != null) && (dt.Rows[i]["ShopAddress"].ToString() != ""))
                    {
                        item.ShopAddress = dt.Rows[i]["ShopAddress"].ToString();
                    }
                    if ((dt.Rows[i]["ShopRemark"] != null) && (dt.Rows[i]["ShopRemark"].ToString() != ""))
                    {
                        item.ShopRemark = dt.Rows[i]["ShopRemark"].ToString();
                    }
                    if ((dt.Rows[i]["ShopCreateTime"] != null) && (dt.Rows[i]["ShopCreateTime"].ToString() != ""))
                    {
                        item.ShopCreateTime = DateTime.Parse(dt.Rows[i]["ShopCreateTime"].ToString());
                    }
                    if ((dt.Rows[i]["ShopState"] != null) && (dt.Rows[i]["ShopState"].ToString() != ""))
                    {
                        if ((dt.Rows[i]["ShopState"].ToString() == "1") || (dt.Rows[i]["ShopState"].ToString().ToLower() == "true"))
                        {
                            item.ShopState = true;
                        }
                        else
                        {
                            item.ShopState = false;
                        }
                    }
                    if ((dt.Rows[i]["ShopPrintTitle"] != null) && (dt.Rows[i]["ShopPrintTitle"].ToString() != ""))
                    {
                        item.ShopPrintTitle = dt.Rows[i]["ShopPrintTitle"].ToString();
                    }
                    if ((dt.Rows[i]["ShopPrintFoot"] != null) && (dt.Rows[i]["ShopPrintFoot"].ToString() != ""))
                    {
                        item.ShopPrintFoot = dt.Rows[i]["ShopPrintFoot"].ToString();
                    }
                    if ((dt.Rows[i]["ShopSmsName"] != null) && (dt.Rows[i]["ShopSmsName"].ToString() != ""))
                    {
                        item.ShopSmsName = dt.Rows[i]["ShopSmsName"].ToString();
                    }
                    if ((dt.Rows[i]["SettlementInterval"] != null) && (dt.Rows[i]["SettlementInterval"].ToString() != ""))
                    {
                        item.SettlementInterval = int.Parse(dt.Rows[i]["SettlementInterval"].ToString());
                    }
                    if ((dt.Rows[i]["ShopProportion"] != null) && (dt.Rows[i]["ShopProportion"].ToString() != ""))
                    {
                        item.ShopProportion = decimal.Parse(dt.Rows[i]["ShopProportion"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Delete(int ShopID)
        {
            return this.dal.Delete(ShopID);
        }

        public bool DeleteList(string ShopIDlist)
        {
            return this.dal.DeleteList(ShopIDlist);
        }

        public bool Exists(int ShopID)
        {
            return this.dal.Exists(ShopID);
        }

        public bool Exists(string shopName)
        {
            return this.dal.Exists(shopName);
        }

        public bool Exists(string shopName, int shopID)
        {
            return this.dal.Exists(shopName, shopID);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public DataSet GetListS(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetListS(PageSize, PageIndex, strWhere, out resCount);
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetListSP(PageSize, PageIndex, strWhere, out resCount);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public Chain.Model.SysShop GetModel(int ShopID)
        {
            return this.dal.GetModel(ShopID);
        }

        public List<Chain.Model.SysShop> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public DataSet GetShop(params string[] strWhere)
        {
            return this.dal.GetShop(strWhere);
        }

        public DataSet GetTotalRptData(string memWhere, string rechargeWhere, string orderWhere, string countWhere, string memstoragetimingWhere, string drawmoneyWhere)
        {
            return this.dal.GetTotalRptData(memWhere, rechargeWhere, orderWhere, countWhere, memstoragetimingWhere, drawmoneyWhere);
        }

        public DataSet getTotalShop(string dtStartTime, string dtEndTime, params string[] strWhere)
        {
            return this.dal.getTotalShop(dtStartTime, dtEndTime, strWhere);
        }

        public int Update(Chain.Model.SysShop model)
        {
            if (this.Exists(model.ShopName, model.ShopID))
            {
                return -1;
            }
            return this.dal.Update(model);
        }

        public int Update(int shopid, int smsType, int pointType)
        {
            return this.dal.Update(shopid, smsType, pointType);
        }
    }
}

